package com.ruoyi.web.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.pojo.ModelSwitchPush;
import com.ruoyi.system.mapper.SysRouteAxesMapper;
import com.ruoyi.system.mapper.SysRouteMapper;
import com.ruoyi.system.service.SysBoatEventlogService;
import com.ruoyi.system.service.SysTaskService;
import com.ruoyi.web.common.Const;
import com.ruoyi.web.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ProjectName: RuoYi-Vue
 * @Package: com.ruoyi.web.service
 * @ClassName: WisdomCruiseServiceImpl
 * @Description: 对组合无人船的控制
 * @Author: mengxiabing
 * @CreateDate: 2022-09-20 14:21
 * @UpdateUser: 更新者
 * @UpdateDate: 2022-09-20 14:21
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Slf4j
@Service
public class WisdomCruiseServiceImpl implements IWisdomCruiseService {
    @Autowired
    private DeviceService deviceService;

    @Resource
    SysTaskService sysTaskService;

    @Resource
    SysRouteAxesMapper sysRouteAxesMapper;

    @Resource
    SysRouteMapper sysRouteMapper;

    @Resource
    SysBoatEventlogService sysBoatEventlogService;

    /**
     * 请求控制
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult requestControl(RequestControlParam param) {
        SysTask sysTask = sysTaskService.getById(param.getTaskId());
        if (Objects.isNull(sysTask)) {
            return AjaxResult.error("任务不存在");
        }
//        deviceService.pushControlStatus(param.getControlType() == null ? 0 : param.getControlType());
        applyControl(param.getControlType() == null ? 0 : param.getControlType(), param.getControl() == null ? 0 : param.getControl());
        return AjaxResult.success();
    }

    /**
     * 改变船方向
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult changeBoatDirection(BoatDirectionParam param) {
        SysTask sysTask = sysTaskService.getById(param.getTaskId());
        if (Objects.isNull(sysTask)) {
            return AjaxResult.error("任务不存在");
        }
        sysTask.setModelType(22);
        sysTaskService.updateById(sysTask);

        SysBoatEventlog sysBoatEventlog = new SysBoatEventlog();
        sysBoatEventlog.setTaskId(sysTask.getId());
        sysBoatEventlog.setCommandText("遥控");
        sysBoatEventlog.setCruiseId(sysTask.getObjectId());
        sysBoatEventlog.setCreateTime(DateUtil.date());
        sysBoatEventlogService.save(sysBoatEventlog);

        ModelSwitchPush modelSwitchPush = new ModelSwitchPush();
        modelSwitchPush.setTaskId(sysTask.getId());
        modelSwitchPush.setModelType(sysTask.getModelType());
        deviceService.sendMessage("modelSwitch", modelSwitchPush);

        deviceService.changeBoatDirection(param);
        return AjaxResult.success();
    }

    /**
     * 切换智慧巡河任务模式
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult switchTaskModel(SysTask param) {
        SysTask sysTask = sysTaskService.getById(param.getId());
        if (Objects.isNull(sysTask)) {
            return AjaxResult.error("任务不存在");
        }
        if (param.getModelType().equals(2)) {
//            stopTask(sysTask);
            deviceService.stopTimer(Const.DEVICE_MQTT_JP_TOPIC);
            deviceService.stopSimulationRemote(Const.DEVICE_MQTT_JP_TOPIC, null);
            ChatBase chatBase = deviceService.pushTaskRouteAxes(sysRouteMapper, sysRouteAxesMapper, sysTask, param);
            deviceService.parasChat(String.valueOf(sysTask.getUserId()), chatBase);
        } else {
            startTask(sysTask);
        }
        return AjaxResult.success();
    }

    /**
     * 开始巡河任务
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult startTask(SysTask param) {
        return getAjaxResult(param, 1, null);
    }

    /**
     * 获取构建向船发送的MQTT数据
     *
     * @param param
     * @param i
     * @param boatDirectionParam
     * @return
     */
    private AjaxResult getAjaxResult(SysTask param, int i, BoatDirectionParam boatDirectionParam) {
        String model = "net2MqttJG1";
        if (param.getModelType().equals(2)) {
            model = "net2MqttJG2";
        }
        ChatBase chatBase = new ChatBase();
        Net2MqttJG1 net2MqttJG1 = new Net2MqttJG1();
        net2MqttJG1.setRecv_type(1);
        net2MqttJG1.setUser_Command(0);
        net2MqttJG1.setCommand_mode(param.getModelType());
        net2MqttJG1.setCommand_Fx(0.0f);
        net2MqttJG1.setCommand_Fy(0.0f);
        net2MqttJG1.setCommand_Mz(0.0f);
        net2MqttJG1.setThruster_PORT_rpm(0);
        net2MqttJG1.setThruster_STBD_rpm(0);
        net2MqttJG1.setThruster_PORT_ang(0);
        net2MqttJG1.setThruster_STBD_ang(0);
        if (boatDirectionParam != null) {
            net2MqttJG1.setCommand_Fx(boatDirectionParam.getCommand_Fx());
            net2MqttJG1.setCommand_Mz(boatDirectionParam.getCommand_Mz());
            net2MqttJG1.setThruster_PORT_rpm(boatDirectionParam.getThruster_PORT_rpm());
            net2MqttJG1.setThruster_STBD_rpm(boatDirectionParam.getThruster_STBD_rpm());
            net2MqttJG1.setThruster_PORT_ang(boatDirectionParam.getThruster_PORT_ang());
            net2MqttJG1.setThruster_STBD_ang(boatDirectionParam.getThruster_STBD_ang());
        }
        net2MqttJG1.setStatus(i);
        net2MqttJG1.setUserId(param.getUserId());
        net2MqttJG1.setBoatId(param.getObjectId());
        String content = JSONObject.toJSONString(net2MqttJG1);
        chatBase.setModule(model);
        chatBase.setContent(content);
        deviceService.parasChat(String.valueOf(param.getUserId()), chatBase);
        return AjaxResult.success();
    }

    /**
     * 结束巡河任务
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult stopTask(SysTask param) {
        SysTask sysTask = sysTaskService.getById(param.getId());
        if (Objects.isNull(sysTask)) {
            return AjaxResult.error("任务不存在");
        }
        //停止定时器
        deviceService.stopTimer(Const.DEVICE_MQTT_JP_TOPIC);
        deviceService.stopSimulationRemote(Const.DEVICE_MQTT_JP_TOPIC, null);
        //推送任务结束
        TaskPush taskPush = new TaskPush();
        taskPush.setId(param.getId().intValue());
        deviceService.sendMessage("closeTask", taskPush);
        deviceService.setSysTask(null);
        return getAjaxResult(sysTask, 0, null);
    }

    /**
     * 请求控制
     *
     * @param isCloudControlAllowed
     * @param control
     * @return
     */
    @Override
    public AjaxResult applyControl(int isCloudControlAllowed, int control) {
        deviceService.applyControl(isCloudControlAllowed, control);
        return AjaxResult.success();
    }

    /**
     * 急停巡河任务
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult emergencyStopTask(SysTask param) {
        SysTask sysTask = sysTaskService.getById(param.getId());
        if (Objects.isNull(sysTask)) {
            return AjaxResult.error("任务不存在");
        }

        sysTask.setModelType(21);
        sysTaskService.updateById(sysTask);

        SysBoatEventlog sysBoatEventlog = new SysBoatEventlog();
        sysBoatEventlog.setTaskId(sysTask.getId());
        sysBoatEventlog.setCommandText("停止");
        sysBoatEventlog.setCruiseId(sysTask.getObjectId());
        sysBoatEventlog.setCreateTime(DateUtil.date());
        sysBoatEventlogService.save(sysBoatEventlog);

        ModelSwitchPush modelSwitchPush = new ModelSwitchPush();
        modelSwitchPush.setTaskId(sysTask.getId());
        modelSwitchPush.setModelType(sysTask.getModelType());
        deviceService.sendMessage("modelSwitch", modelSwitchPush);

        deviceService.emergencyStopTask();
        return AjaxResult.success();
    }

    /**
     * 用户上线
     *
     * @param number
     * @return
     */
    @Override
    public AjaxResult onlineUser(int number) {
        OnlineUser onlineUser = new OnlineUser();
        onlineUser.setOnlineUser(number);
        deviceService.sendMessage(Const.ONLINE_USER, onlineUser);
        return AjaxResult.success();
    }

    /**
     * 推送未结束任务
     *
     * @param number
     * @return
     */
    @Override
    public AjaxResult notFinishedTask(int number) {
        NotFinishedTask notFinishedTask = new NotFinishedTask();
        notFinishedTask.setNotFinishedTask(number);
        deviceService.sendMessage(Const.NOT_FINISHED_TASK, notFinishedTask);
        return AjaxResult.success();
    }

    /**
     * 推送在线的无人船
     *
     * @param number
     * @return
     */
    @Override
    public AjaxResult onlineBoat(int number) {
        OnlineBoat onlineBoat = new OnlineBoat();
        onlineBoat.setOnlineBoat(number);
        deviceService.sendMessage(Const.ONLINE_BOAT, onlineBoat);
        return AjaxResult.success();
    }

    /**
     * 切换污染源模式
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult switchToPollute(SysTaskPollute param) {
        ChatBase chatBase = deviceService.pushTaskPollute(param);
        deviceService.parasChat("1", chatBase);

        SysTask sysTask = sysTaskService.getById(param.getTaskId());
        if (Objects.isNull(sysTask)) {
            return AjaxResult.error("任务不存在");
        }

        sysTask.setModelType(24);
        sysTaskService.updateById(sysTask);

        SysBoatEventlog sysBoatEventlog = new SysBoatEventlog();
        sysBoatEventlog.setTaskId(sysTask.getId());
        sysBoatEventlog.setCommandText("污染源模式");
        sysBoatEventlog.setCruiseId(sysTask.getObjectId());
        sysBoatEventlog.setCreateTime(DateUtil.date());
        sysBoatEventlogService.save(sysBoatEventlog);

        ModelSwitchPush modelSwitchPush = new ModelSwitchPush();
        modelSwitchPush.setTaskId(sysTask.getId());
        modelSwitchPush.setModelType(sysTask.getModelType());
        deviceService.sendMessage("modelSwitch", modelSwitchPush);
        return AjaxResult.success();
    }
}
